package com.cqhfan.imservice.mq;

import cn.hutool.json.JSONUtil;
import com.cqhfan.core.constans.MqConstants;
import com.cqhfan.imservice.service.ChatGroupService;
import com.cqhfan.imservice.service.ImMessageService;
import com.cqhfan.pojo.im.dto.WebSocketMessageDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 眭传洪
 * @create 2025-08-17 1:194
 */
@Slf4j
@Component
public class ImMessageListener {

    @Resource
    private ChatGroupService chatGroupService;

    @Resource
    private ImMessageService imMessageService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.EXCHANGE.CHAT_CREATE_GROUP_EXCHANGE, type = ExchangeTypes.FANOUT),
            value = @Queue(value = MqConstants.QUEUE.CREATE_GROUP_NOTICE_QUEUE)
    ))
    public void listenChatGroupCreate(WebSocketMessageDTO message) {
        log.info("mq接收到创建群聊的消息, msg = {}", message);
        chatGroupService.noticeChatGroupCreateMsg(JSONUtil.toJsonStr(message));
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.EXCHANGE.CHAT_SEND_EXCHANGE, type = ExchangeTypes.FANOUT),
            value = @Queue(value = MqConstants.QUEUE.CHAT_MSG_SEND_QUEUE)
    ))
    public void listenSendMsg(WebSocketMessageDTO message) {
        log.info("mq接收到聊天消息的数据, msg = {}", message);
        imMessageService.listenSendMsg(JSONUtil.toJsonStr(message));
    }

}
